package bluejack.game.avg;
import bluejack.game.engine.Scene;
import bluejack.operation.DelayOper;
import bluejack.operation.FunctionOper;
import bluejack.operation.LoadOper;
import bluejack.operation.Queue;
import bluejack.operation.URLLoadOper;
import nme.display.Bitmap;
import nme.Lib;

/**
 * ...
 * @author BlueJack
 */

class AVGScene extends Scene
{
	private var queue:Queue;
	private var images:Hash<Bitmap>;

	public function new() {
		super();
		queue = new Queue();
		images = new Hash();
	}
	
	override public function onEnter():Void {
		enterScript();
	}
	
	private function enterScript():Void {
		
	}
	
	public function wait(time:Float):Void {
		new DelayOper(3).commit(queue);
	}
	
	public function showImage(path:String):Void {
		if (!images.exists(path)) {
			var loader:LoadOper = new LoadOper(path);
			loader.commit(queue);
			new FunctionOper(getImage, [path, loader]).commit(queue);
		}
		new FunctionOper(_showImage, [path]).commit(queue);
	}
	
	private function getImage(path:String, loader:LoadOper):Void {
		var img:Bitmap = cast(loader.getData(), Bitmap);
		images.set(path, img);
	}
	
	private function _showImage(path:String):Void {
		var img:Bitmap = images.get(path);
		addChild(img);
	}
}